package com.guanyong.services;

import com.guanyong.dao.*;
import com.guanyong.model.*;
import com.guanyong.utils.Comm.ResultCode;
import com.guanyong.utils.Comm.ResultObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * Created by mxk on 2017/6/15.
 */
@Service
public class VideoCountService {

    @Autowired
    IVideoLookLogDaoMapper iVideoLookLogDaoMapper;

    @Autowired
    IVideoCollectLogDaoMapper iVideoCollectLogDaoMapper;

    @Autowired
    IVideoGoodLogDaoMapper iVideoGoodLogDaoMapper;

    @Autowired
    IVideoShareLogDaoMapper iVideoShareLogDaoMapper;

    @Autowired
    IVideoPlayLogDaoMapper iVideoPlayLogDaoMapper;

    @Autowired
    IVideoBuyLogDaoMapper iVideoBuyLogDaoMapper;

    @Autowired
    FilmServices filmServices;

    @Autowired
    ObjectClassTypeService objectClassTypeService;

    //返回接口
    IObjectUpdateCount UpdateCounLog(Integer classType){

        if (classType==1){
            return  filmServices;
        }
        else{
            return null;
        }
    }

  //点击
    public ResultObject<Integer> AddLookLog(Integer classtype,
                                   String userid,
                                   String objectid,
                                   String ip, Integer
            apptype){
        //判断是否有记录

        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        Integer res=-1;
        VideoLookLogModel videoLookLogModel=new VideoLookLogModel();
        videoLookLogModel.setfClasstype(classtype);
        videoLookLogModel.setfUserid(userid);
        videoLookLogModel.setfApptype(apptype);
        videoLookLogModel.setfIp(ip);
        videoLookLogModel.setfObjectid(objectid);
        res =iVideoLookLogDaoMapper.insert(videoLookLogModel);
        if (res >-1){
            //添加数量
            int res1= UpdateCounLog(classtype).UpdateObjectLookCount(objectid);
            if (res1 >-1){
                resultObject.setCode(ResultCode.OperateOk);
            }
            else{
                resultObject.setCode(ResultCode.OperateFail);
                resultObject.setErrorMsg("修改数量失败！");
            }
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
            resultObject.setErrorMsg("添加浏览记录失败！");
        }
        return resultObject;
    }


    //点赞
    public  ResultObject<Integer> AddGoodLog(Integer classtype,String userid,String objectid,String ip,Integer
            apptype){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();

        VideoGoodLogModel videoLookLogModel=new VideoGoodLogModel();
        videoLookLogModel.setfClasstype(classtype);
        videoLookLogModel.setfUserid(userid);
        videoLookLogModel.setfApptype(apptype);
        videoLookLogModel.setfIp(ip);
        videoLookLogModel.setfObjectid(objectid);
        int  res =iVideoGoodLogDaoMapper.insert(videoLookLogModel);
        if (res >-1){
            //添加数量
            Integer res1=  UpdateCounLog(classtype).UpdateObjectGoodCount(objectid);
            if (res1 >-1){
                resultObject.setCode(ResultCode.OperateOk);
            }
            else{
                resultObject.setCode(ResultCode.OperateFail);
                resultObject.setErrorMsg("修改数量失败！");
            }
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
            resultObject.setErrorMsg("添加点赞记录失败！");
        }
        return resultObject;
    }

    //分享
    public  ResultObject<Integer> AddShareLog(Integer classtype,String userid,String objectid,String ip,Integer
            apptype){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        Integer res=-1;
        VideoShareLogModel videoShareLogModel=new VideoShareLogModel();
        videoShareLogModel.setfClasstype(classtype);
        videoShareLogModel.setfUserid(userid);
        videoShareLogModel.setfApptype(apptype);
        videoShareLogModel.setfIp(ip);
        videoShareLogModel.setfObjectid(objectid);
        res =iVideoShareLogDaoMapper.insert(videoShareLogModel);
        if (res >-1){
            //添加数量
            UpdateCounLog(classtype).UpdateObjectShareCount(objectid);
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
            resultObject.setErrorMsg("添加分享记录失败！");
        }
        return resultObject;
    }

    //添加收藏
    public   ResultObject<Integer>  AddCollectLog(Integer classtype,String userid,String objectid,String ip,Integer
            apptype){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        VideoCollectLogModel videoCollectLogModel1=iVideoCollectLogDaoMapper.GetCollectModel
                (classtype,
                objectid,userid);
        if (videoCollectLogModel1!=null){
            resultObject.setCode(ResultCode.FiledRepeat);
            resultObject.setErrorMsg("已经添加过此记录！");
        }
        else{
            Integer res=-1;

                VideoCollectLogModel videoCollectLogModel=new VideoCollectLogModel();
                videoCollectLogModel.setfClasstype(classtype);
                videoCollectLogModel.setfUserid(userid);
                videoCollectLogModel.setfApptype(apptype);
                videoCollectLogModel.setfIp(ip);
                videoCollectLogModel.setfObjectid(objectid);
                res =iVideoCollectLogDaoMapper.insert(videoCollectLogModel);
                if (res >-1){
                    //添加数量
                    int res1= UpdateCounLog(classtype).UpdateObjectCollectCountAdd(objectid);
                    if (res1 >-1){
                        resultObject.setCode(ResultCode.OperateOk);
                    }
                    else{
                        resultObject.setCode(ResultCode.OperateFail);
                        resultObject.setErrorMsg("修改数量失败！");
                    }
                }
                else{
                    resultObject.setCode(ResultCode.OperateFail);
                    resultObject.setErrorMsg("添加收藏失败！");
                }


        }

        return resultObject;
    }

    //取消收藏
    public   ResultObject<Integer>  DelCollectLog(Integer fid){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        VideoCollectLogModel videoCollectLogModel1=iVideoCollectLogDaoMapper.GetCollectModelById
                (fid);
        if (videoCollectLogModel1==null){
            resultObject.setCode(ResultCode.FiledRepeat);
            resultObject.setErrorMsg("没有此记录！");
        }
        else{
            Integer res=-1;

            res =iVideoCollectLogDaoMapper.delete(fid);
            if (res >-1){
                //添加数量
                int res1= UpdateCounLog(videoCollectLogModel1.getfClasstype()).
                        UpdateObjectCollectCountdel
                        (videoCollectLogModel1.getfObjectid());
                if (res1 >-1){
                    resultObject.setCode(ResultCode.OperateOk);
                }
                else{
                    resultObject.setCode(ResultCode.OperateFail);
                    resultObject.setErrorMsg("修改数量失败！");
                }
            }
            else{
                resultObject.setCode(ResultCode.OperateFail);
                resultObject.setErrorMsg("添加收藏失败！");
            }
        }

        return resultObject;
    }


    //删除播放记录（多个）
    public  ResultObject<Integer> DelCollectLogIdList(String  fidList){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        StringBuilder stringBuilder=new StringBuilder();
        String [] strList = fidList.split(",");
        if (strList !=null){
            for (String id:strList){
                if (id!=null &&!("".equals(id))){
                    stringBuilder.append(",");
                    stringBuilder.append("'");
                    stringBuilder.append(id);
                    stringBuilder.append("'");
                }
            }
            Integer res=-1;
            res =iVideoCollectLogDaoMapper.deleteIdlist(stringBuilder.deleteCharAt(0).toString());
            if (res >-1){
                int res1= filmServices.UpdateObjectCollectCountdelList(stringBuilder.toString());
                if (res1 >-1){
                    resultObject.setCode(ResultCode.OperateOk);
                }
                else{
                    resultObject.setCode(ResultCode.OperateFail);
                    resultObject.setErrorMsg("修改数量失败！");
                }
            }
            else{
                resultObject.setCode(ResultCode.OperateFail);
                resultObject.setErrorMsg("操作数据失败！");
            }
        }
        return resultObject;
    }

    //取消收藏
    public   ResultObject<Integer>  DelCollectLogByObjectid(String userid,String objectid){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        VideoCollectLogModel videoCollectLogModel1=iVideoCollectLogDaoMapper.GetCollectModelByObjectId
                (userid,objectid);
        if (videoCollectLogModel1==null){
            resultObject.setCode(ResultCode.FiledRepeat);
            resultObject.setErrorMsg("没有此记录！");
        }
        else{
            Integer res=-1;

            res =iVideoCollectLogDaoMapper.delete(videoCollectLogModel1.getfId());
            if (res >-1){
                //添加数量
                int res1= UpdateCounLog(videoCollectLogModel1.getfClasstype()).
                        UpdateObjectCollectCountdel
                                (videoCollectLogModel1.getfObjectid());
                if (res1 >-1){
                    resultObject.setCode(ResultCode.OperateOk);
                }
                else{
                    resultObject.setCode(ResultCode.OperateFail);
                    resultObject.setErrorMsg("修改数量失败！");
                }
            }
            else{
                resultObject.setCode(ResultCode.OperateFail);
                resultObject.setErrorMsg("添加收藏失败！");
            }
        }

        return resultObject;
    }


    public ResultObject<Integer> DelCollectLogByObjectList(String userid,String idliststr)
    {
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        if (idliststr!=null && idliststr!=""){
            String[] arys=idliststr.split(",");
            for (String  ary:arys
                 ) {
                resultObject=  DelCollectLogByObjectid(userid,ary);
            }
        }
       else{
            resultObject.setCode(ResultCode.ParamsError);
        }
        return  resultObject;
    }

    public  VideoCollectLogModel GetVideoCollectByUserId(String videoid,String userid){

        return iVideoCollectLogDaoMapper.GetCollectModelByObjectId(userid,videoid);
    }

    //获取收藏列表
    public HashMap<String,Object> GetCollectList(
            String  userid,
            Integer pno,
            Integer pagesize,
            Integer allcount){
        HashMap<String,Object> hashMap=new HashMap();
        if (allcount<0){
            Integer count=iVideoCollectLogDaoMapper
                    .SelectCollectListCount(userid);
            hashMap.put("count",count);
        }
        else {
            hashMap.put("count",allcount);
        }
        List<VideoCollectLogModel> videoCollectLogModelList
                =iVideoCollectLogDaoMapper.SelectCollectList(userid,
                (pno-1)*pagesize,pagesize);

        for (VideoCollectLogModel videoCollectLogModel :videoCollectLogModelList){
            Object object= objectClassTypeService.GetObject(videoCollectLogModel.getfClasstype(),
                    videoCollectLogModel.getfObjectid());
            if (object!=null) {
                videoCollectLogModel.setfObject(object);
            }

        }
        hashMap.put("list",videoCollectLogModelList);
        return hashMap;
    }


    //播放
    public  ResultObject<Integer> AddPlayLog(Integer classtype,String userid,String phoneSN,String objectid,String ip,Integer
            apptype,Integer playtime){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        Integer res=-1;
        VideoPlayLogModel videoPlacyLogModel1=iVideoPlayLogDaoMapper.GetPlayModel(classtype,
                objectid,userid);
        if (videoPlacyLogModel1!=null){
            res =iVideoPlayLogDaoMapper.updateIntime(videoPlacyLogModel1.getfId());
            if (res >-1){
                //添加数量
                Integer res1 = UpdateCounLog(classtype).UpdateObjectPlayCount(objectid);
                if (res1 >-1){
                    resultObject.setCode(ResultCode.OperateOk);
                }
                else{
                    resultObject.setCode(ResultCode.OperateFail);
                    resultObject.setErrorMsg("修改数量失败！");
                }
            }
            else{
                resultObject.setCode(ResultCode.OperateFail);
                resultObject.setErrorMsg("添加播放记录失败！");
            }
        }
        else{
            VideoPlayLogModel videoPlayLogModel=new VideoPlayLogModel();
            videoPlayLogModel.setfClasstype(classtype);
            videoPlayLogModel.setfUserid(userid);
            videoPlayLogModel.setfPhoneSN(phoneSN);
            videoPlayLogModel.setfApptype(apptype);
            videoPlayLogModel.setfIp(ip);
            videoPlayLogModel.setfObjectid(objectid);
            videoPlayLogModel.setfPalytime(playtime);
            res =iVideoPlayLogDaoMapper.insert(videoPlayLogModel);
            if (res >-1){
                //添加数量
                Integer res1 = UpdateCounLog(classtype).UpdateObjectPlayCount(objectid);
                if (res1 >-1){
                    resultObject.setCode(ResultCode.OperateOk);
                }
                else{
                    resultObject.setCode(ResultCode.OperateFail);
                    resultObject.setErrorMsg("修改数量失败！");
                }
            }
            else{
                resultObject.setCode(ResultCode.OperateFail);
                resultObject.setErrorMsg("添加播放记录失败！");
            }
        }
        return resultObject;
    }

    //删除播放记录
    public  ResultObject<Integer> DelPlayLog(Integer fid){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        Integer res=-1;
        res =iVideoPlayLogDaoMapper.delete(fid);
        if (res >-1){
            resultObject.setCode(ResultCode.OperateOk);
        }
        else{
            resultObject.setCode(ResultCode.OperateFail);
            resultObject.setErrorMsg("操作数据失败！");
        }
        return resultObject;
    }

    //删除播放记录
    public  ResultObject<Integer> DelPlayLogIdlist(String suerid,String  fidList){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        StringBuilder stringBuilder=new StringBuilder();
        String [] strList = fidList.split(",");
        if (strList !=null){
            for (String id:strList){
                if (id!=null &&!("".equals(id))){
                    stringBuilder.append(",");
                    stringBuilder.append("'");
                    stringBuilder.append(id);
                    stringBuilder.append("'");
                }
            }
            Integer res=-1;
            res =iVideoPlayLogDaoMapper.deleteIdlist(suerid,stringBuilder.deleteCharAt(0).toString());
            if (res >-1){
                resultObject.setCode(ResultCode.OperateOk);
            }
            else{
                resultObject.setCode(ResultCode.OperateFail);
                resultObject.setErrorMsg("操作数据失败！");
            }
        }
        return resultObject;
    }


    //获取收藏列表
    public HashMap<String,Object> GetPlayList(
            String  userid,
            String  phoneSN,
            Integer isweek,
            Integer offset,
            Integer limit,
            Integer allcount){
        HashMap<String,Object> hashMap=new HashMap();
        if (allcount<0){
            Integer count=iVideoPlayLogDaoMapper
                    .SelectPlayListCount(userid,phoneSN,isweek);
            hashMap.put("count",count);
        }
        else {
            hashMap.put("count",allcount);
        }
        List<VideoPlayLogModel> videoPlayLogModelList
                =iVideoPlayLogDaoMapper.SelectPlayList(userid,phoneSN,isweek,
                offset,limit);

        for (VideoPlayLogModel videoPlayLogModel :videoPlayLogModelList){
            Object object= objectClassTypeService.GetObject(videoPlayLogModel
                            .getfClasstype(),
                    videoPlayLogModel.getfObjectid());
            if (object!=null) {
                videoPlayLogModel.setfObject(object);
            }

        }
        hashMap.put("list",videoPlayLogModelList);
        return hashMap;
    }


    public HashMap<String,Object> GetPlayListLog(
            String  userid,
            String  phoneSN,
            Integer pno,
            Integer pagesize,
            Integer allcount){
        HashMap<String,Object> hashMap=new HashMap();
        if (allcount<0){
            Integer count=iVideoPlayLogDaoMapper
                    .SelectPlayListLogCount(userid,phoneSN);
            hashMap.put("count",count);
        }
        else {
            hashMap.put("count",allcount);
        }
        List<VideoPlayLogModel> videoPlayLogModelList
                =iVideoPlayLogDaoMapper.SelectPlayListLog(userid,phoneSN,
                (pno-1)*pagesize,pagesize);

        for (VideoPlayLogModel videoPlayLogModel :videoPlayLogModelList){
            Object object= objectClassTypeService.GetObject(videoPlayLogModel
                            .getfClasstype(),
                    videoPlayLogModel.getfObjectid());
            if (object!=null) {
                videoPlayLogModel.setfObject(object);
            }

        }
        hashMap.put("list",videoPlayLogModelList);
        return hashMap;
    }

}
